﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tank.Properties;

namespace Tank
{
    class EnemyTank : Moveing
    {
        private Random r = new Random();
        private object _lock = new object();
        // 子弹发射的速度
        public int AttackSpeed { get; set; }
        private int attackCount = 0;
        public int ChangeDirSpeed { get; set; }
        private int changeDirCount = 0;
        public EnemyTank(int X, int y, int speet,
          Bitmap bmpDown, Bitmap bmpUp, Bitmap bmpRight,
          Bitmap bmpLeft
            )
        {
            this.X = X;
            this.Y = y;
            this.Speet = speet;

            this.BitmapDown = bmpDown;
            this.BitmapLeft = bmpLeft;
            this.BitmapRight = bmpRight;
            this.BitmapUp = bmpUp;
            this.Dir = Direction.down;
            AttackSpeed = 30;
            ChangeDirSpeed = 40;
        }
        public override void Update()
        {

            MoveCheck();
            Move();
            AttackCheck();
            AutoChangeDirection();
            base.Update();
        }
        // 坦克越界检查
        private void MoveCheck()
        {
        

           
            #region 检测是否越界
            if (Dir == Direction.Up)
            {
                if (Y - Speet < 0)
                {
                    ChangeDirection();
                    return;
                }
            }
            else if (Dir == Direction.down)
            {
                if (Y + Speet + Height > 450)
                {
                    ChangeDirection();
                    return;
                }

            }
            else if (Dir == Direction.Left)
            {
                if (X - Speet < 0)
                {
                    ChangeDirection();
                    return;
                }
            }
            else
            {
                if (X + Speet + Width > 450)
                {
                    ChangeDirection();
                    return;
                }
                
            }
            #endregion
            Rectangle rect = GetRectangle();
            switch (Dir)
            {
                case Direction.Up:
                    rect.Y -= Speet;
                    break;
                case Direction.down:
                    rect.Y += Speet;
                    break;
                case Direction.Left:
                    rect.X -= Speet;
                    break;
                case Direction.right:
                    rect.X += Speet;
                    break;
            }
                // 两种墙体都不能碰撞
                if (GameObjectManager.IsCollidedWall(rect) != null)
                {
                    ChangeDirection(); return;
                }
                if (GameObjectManager.IsCollidedSteel(rect) != null)
                {
                    ChangeDirection(); return;
                }
                if (GameObjectManager.IsCollidedBoss(rect))
                {
                    ChangeDirection(); return;
                }
            
        }
        // 改变坦克朝向
        private void ChangeDirection() {
            lock (_lock) { 
           while(true)
            {
                Direction dir = (Direction)r.Next(0, 4);
                if(dir == Dir)
                {
                    continue;
                }
                Dir = dir;
                break;
            }
            MoveCheck();
            }
        }
        // 坦克移动
        private void Move()
        {
          
            switch (Dir)
            {
                case Direction.Up:
                    Y -= Speet;
                    break;
                case Direction.down:
                    Y += Speet;
                    break;
                case Direction.Left:
                    X -= Speet;
                    break;
                case Direction.right:
                    X += Speet;
                    break;
            }
        }
        // 检查子弹是否需要攻击
        private void AttackCheck()
        {
            attackCount++;
            if (attackCount < AttackSpeed) return;
            Attack();
            attackCount = 0;
        }
        // 子弹攻击
        private void Attack()
        {
            int x = this.X;
            int y = this.Y;
            switch (Dir)
            {
                case Direction.Up:
                    x = x + this.Width / 2;
                    break;
                case Direction.down:
                    x = x + Width / 2;
                    y += Height;
                    break;
                case Direction.Left:
                    y = y + Height / 2;
                    break;
                case Direction.right:
                    x += Width;
                    y = y + Height / 2;
                    break;
            }

            GameObjectManager.CreateBullet(x, y, Tag.EnemyTank, Dir);
        }
        // 敌人自动调转方向
        private void AutoChangeDirection()
        {
            changeDirCount++;
            if (changeDirCount < ChangeDirSpeed) return;
            changeDirCount = 0;
        }
    }

}
